<?php

use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\widgets\ActiveForm;
use common\models\FormVal;
use backend\models\Supplier;
use backend\models\Delivery;

/* @var $this yii\web\View */
/* @var $model backend\models\Supplier */
/* @var $form yii\widgets\ActiveForm */

$shippingTypes = [Supplier::SHIPPING_TYPE_DELIVERY => '配送中心发货',  Supplier::SHIPPING_TYPE_SELF => '自发货'];
?>

<div class="supplier-form">

    <?php $form = ActiveForm::begin(); ?>

    <?= $form->field($model, 'name')->textInput(['maxlength' => 45]) ?>
    
    <?= $form->field($model, 'code')->hint('新建后，供应商代码将不能修改，请慎重！')->textInput(['maxlength' => 4]); ?>
    
    <?= $form->field($model, 'shipping_type')->radioList($shippingTypes, ['separator' => FormVal::$separator10]); ?>
    
    <div id="isList">
        <?= $form->field($model, 'delivery_id')->dropDownList(ArrayHelper::map(Delivery::find()->all(), 'id', 'name')) ?>
    </div>
    
    <?= $form->field($model, 'enable')->radioList(FormVal::$bool, ['separator' => FormVal::$separator10]); ?>

    <?= $form->field($model, 'deduct')->textInput()->hint('平台向供应商的提成百分比，如：提成2%，就填2') ?>
    
    <?= $form->field($model, 'remark')->textInput(['maxlength' => 100]) ?>

    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? '新建' : '更新', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>

<script>
    window.onload=function(){
        $('#supplier-shipping_type input[value=1]').click(function(){
            $('#isList').css('display','block');
        });
        
        $('#supplier-shipping_type input[value=2]').click(function(){
            $('#isList').css('display','none');
        });
        
        <?php if($model->isNewRecord || (!$model->isNewRecord && $model->shipping_type=  Supplier::SHIPPING_TYPE_SELF)):?>
            $('#isList').css('display','none');
        <?php endif;?>
        
        <?php if(!$model->isNewRecord):?>
            $('#supplier-code').attr('readonly',true);
        <?php endif;?>
    }

</script>
